<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Game extends AdminController{
	public $_validation = null;
	
	function __construct(){
		parent::__construct();
		$this->load->model('MGame');
		$this->_validation = Array(
			array('field'=>'code', 'label'=>'游戏代码', 'rules'=>'trim|required|max_length[64]|callback__checkCode')
			,array('field'=>'name', 'label'=>'游戏名称', 'rules'=>'trim|required|max_length[64]')
			,array('field'=>'description', 'label'=>'游戏描述', 'rules'=>'max_length[255]')
			,array('field'=>'pic', 'label'=>'图片', 'rules'=>'upload[upload_path=game,encrypt_name=true]')
			,array('field'=>'tag_id[]', 'label'=>'游戏标签', 'rules'=>'')
			,array('field'=>'platform', 'label'=>'游戏平台', 'rules'=>'required|integer|max_length[11]')
			,array('field'=>'type', 'label'=>'游戏类型', 'rules'=>'required|integer|max_length[11]')
			,array('field'=>'sort', 'label'=>'排序', 'rules'=>'integer|max_length[11]')
			,array('field'=>'status', 'label'=>'状态', 'rules'=>'integer')
			,array('field'=>'update_time', 'label'=>'更新时间', 'rules'=>'')
		);
	}
	
	function _checkCode($code){
		$where = Array('code'=>$code);
		if(isset($this->editObj) && $this->editObj){
			$where['id <>'] = $this->editObj->id;
		}
		if($this->MGame->getAll($where)){
			$this->form_validation->set_message('_checkCode', '已经存在相同代码的游戏！');
			return false;
		}
		return true;
	}
	
	function index($game_tag=null){
		$this->output->enable_profiler(TRUE);
		$this->load->model('Mtag');
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		$this->formfilter->addFilter('name', 'like');
		$this->formfilter->addFilter('type','where');
		$this->formfilter->addFilter('platform','where');
		$this->formfilter->addFilter('status','where');
		$this->formfilter->addFilter('tag_id','where');
		
		$limit = $this->pagination($this->MGame->getCount());
		
		$list = $this->MGame->getList($limit);
		$tagList = $this->Mtag->getAll();
		$this->assign('tagList', $tagList);
		$this->assign('list', $list);
		$this->layout();
	}
	
	function add(){
		$this->load->model('Mtag');
		$this->load->model('MGameTags');
		$tagList = $this->Mtag->getAll();
		
		$obj = $this->MGame->createVo();
		$selected_tags = Array();
		$this->load->library('form_validation');
		$this->form_validation->set_rules($this->_validation);
		
		
		if($this->form_validation->run()==TRUE){
			$this->load->helper('populate');
			$newObj = populate((Object)((Array)$obj), $this->form_validation->post());
			$post_tag_id_arr = $this->input->post('tag_id');
			if($this->MGame->handleAddTrans($newObj,$post_tag_id_arr)){
				successAndRedirect(l('add_success'));
			}else{
				errorAndRedirect(l('add_faild'));
			}
		}
		$this->assign('tagList', $tagList);
		$this->assign('selectedTags', $selected_tags);
		$this->assign('obj', $obj);
		$this->layout();
	}
	
	function edit($id){
		if(!is_numeric($id)){
			show_error(l('id_not_null'));
		}
		$this->load->model('Mtag');
		$this->load->model('MGameTags');
		$tagList = $this->Mtag->getAll();
		$obj = $this->MGame->getOne(Array('id'=>$id));
		if(!$obj){
			errorAndRedirect('未找到定义！');
		}
		//该游戏选定的tags
		$selected_tags = $this->MGameTags->getAllSelectedTags($id);
		$this->editObj = $obj;
		$this->load->library('form_validation');
		$this->form_validation->set_rules($this->_validation);
		if($this->form_validation->run()==TRUE){
			$this->load->helper('populate');
			$newObj = populate((Object)((Array)$obj), $this->form_validation->post());
			$post_tag_id_arr = $this->input->post('tag_id');
			if($this->MGame->handleEditTrans($id,$newObj,$post_tag_id_arr)){
				successAndRedirect(l('edit_success'));
			}else{
				errorAndRedirect(l('data_fail'));
			}
		}
		$this->assign('tagList', $tagList);
		$this->assign('selectedTags', $selected_tags);
		$this->assign('obj', $obj);
		$this->layout();
	}
	function delete($id, $update_time){
		if(!is_numeric($id) || !is_numeric($update_time)){
			show_error(l('id_not_null'));
		}
		$this->load->model('MGameTags');
		if($this->MGame->handleDeleteTrans($id,$update_time)){
			successAndRedirect(l('delete_success'));
		}else{
			errorAndRedirect(l('data_fail'));
		}
	}
	
	
}